package com.financing.wap.web.controller.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dto.InfoSubscribeDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IInfoSubscribeService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.StringUtil;
import com.financing.wap.web.controller.BaseController;

/**
 * 订阅信息-Controller
 * 
 * @author lisu
 *
 */
@Controller("apiInfoSubscribeController")
@RequestMapping("/api/infoSubscribe")
public class InfoSubscribeController extends BaseController {

	private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IInfoSubscribeService infoSubscribeServiceImpl;

	/**
	 * 订阅信息
	 * @param request
	 * @param response
	 * @param InfoSubscribeDTO 订阅信息DTO
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public ResultMap addInfoSubscribe(HttpServletRequest request,
			HttpServletResponse response,
			InfoSubscribeDTO infoSubscribeDTO) {
		ResultMap result = new ResultMap();
		logger.info("In InfoSubscribeController addInfoSubscribe Begin");

	
		
		// 添加参数验证 ，内容id、内容类型、邮箱地址
		if (StringUtil.isNullOrEmptyStr(infoSubscribeDTO.getSubMail())
				|| StringUtil.isNullOrEmptyStr(infoSubscribeDTO.getSubType())
				|| infoSubscribeDTO.getSubId() == null) {
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
			result.setMessage("参数缺失");
			return result;
		}
		
		try {
			Session session = SessionUtil.getSession();
			UserDTO loginUser = session.getUser();
			InfoSubscribeDTO checkDto =  new InfoSubscribeDTO();
			checkDto.setCreateBy(loginUser.getId().toString());
			checkDto.setSubId(infoSubscribeDTO.getSubId());
			checkDto.setSubType(infoSubscribeDTO.getSubType());
			
			//判断是否已经订阅过
			InfoSubscribeDTO queryDto = infoSubscribeServiceImpl.queryInfoSubscribe(checkDto);
			if (queryDto == null) {
				logger.info("In addInfoSubscribe add type!");
				// 初始化属性
				DTOdateSetUtil.setCreateInfo(infoSubscribeDTO);
				DTOdateSetUtil.setUpdateInfo(infoSubscribeDTO);
				
				infoSubscribeServiceImpl.insertInfoSubscribe(infoSubscribeDTO);
				
			}else{
				logger.info("In addInfoSubscribe update type!");
				infoSubscribeDTO.setId(queryDto.getId());
				DTOdateSetUtil.setUpdateInfo(infoSubscribeDTO);
				infoSubscribeServiceImpl.updateInfoSubscribe(infoSubscribeDTO);
			}
			
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			
			logger.info("In BankCardController addBankCard end");
		} catch (Exception e) {
			logger.error("error BankCardController addBankCard:", e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

}
